﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Proj5715.MaterialRequisition
{
    public partial class MaterialRequisitionDetailEdit : System.Web.UI.Page
    {
        public string ID
        {
            set
            {
                ViewState["ID"] = value;
            }
            get
            {
                return ViewState["ID"] != null ? ViewState["ID"].ToString() : Guid.NewGuid().ToString();
            }
        }

        public string FlowInstanceID
        {
            set
            {
                ViewState["FlowInstanceID"] = value;
            }
            get
            {
                return ViewState["FlowInstanceID"] != null ? ViewState["FlowInstanceID"].ToString() : string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ID = Request.QueryString["id"];
                this.FlowInstanceID = Request.QueryString["FlowInstanceID"];
                InitControlData(this.ID);
            }
        }

        private void InitControlData(string id)
        {
            string sql = @"SELECT * FROM dbo.t5715_MaterialRequisitionDetail WHERE ID = @ID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ID", DbType.String, id);

                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        string materialCode = reader["MaterialCode"] != null ? reader["MaterialCode"].ToString() : string.Empty;
                        txt_MaterialCode.Text = materialCode;
                        hidMaterialCode.Value = materialCode;

                        string materialName = reader["MaterialName"] != null ? reader["MaterialName"].ToString() : string.Empty;
                        txt_MaterialName.Text = materialName;
                        hidMaterialName.Value = materialName;

                        string dispatchCode = reader["DispatchCode"] != null ? reader["DispatchCode"].ToString() : string.Empty;
                        txt_DispatchCode.Text = dispatchCode;
                        hidDispatchCode.Value = dispatchCode;

                        string materialUnit = reader["MaterialUnit"] != null ? reader["MaterialUnit"].ToString() : string.Empty;
                        txt_MaterialUnit.Text = materialUnit;
                        hidMaterialUnit.Value = materialUnit;

                        txt_Num.Text = reader["Num"] != null ? reader["Num"].ToString() : string.Empty;
                        txt_UnitCost.Text = reader["UnitCost"] != null ? reader["UnitCost"].ToString() : string.Empty;
                        txt_Cost.Text = reader["Cost"] != null ? reader["Cost"].ToString() : string.Empty;
                        txt_Purpose.Text = reader["Purpose"] != null ? reader["Purpose"].ToString() : string.Empty;
                        txt_Remark.Text = reader["Remark"] != null ? reader["Remark"].ToString() : string.Empty;
                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void UpdateData()
        {
            string sql_select = @"SELECT COUNT(ID) FROM dbo.t5715_MaterialRequisitionDetail WHERE ID = @ID";
            string sql_insert = @"INSERT INTO dbo.t5715_MaterialRequisitionDetail
                                       ([ID]
                                       ,[FlowInstanceID]
                                       ,[MaterialName]
                                       ,[MaterialCode]
                                       ,[MaterialUnit]
                                       ,[Num]
                                       ,[UnitCost]
                                       ,[Cost]
                                       ,[Purpose]
                                       ,[Remark]
                                       ,[DispatchCode]
                                       ,[MissileCode]
                                       ,[ProcessSortNumber]
                                       ,[MRType])
                                 VALUES
                                       (@ID
                                       ,@FlowInstanceID
                                       ,@MaterialName
                                       ,@MaterialCode
                                       ,@MaterialUnit
                                       ,@Num
                                       ,@UnitCost
                                       ,@Cost
                                       ,@Purpose
                                       ,@Remark
                                       ,@DispatchCode
                                       ,@MissileCode
                                       ,@ProcessSortNumber
                                       ,@MRType)";
            string sql_update = @"UPDATE dbo.t5715_MaterialRequisitionDetail
                                       SET FlowInstanceID = FlowInstanceID
                                          ,MaterialName = @MaterialName
                                          ,MaterialCode = @MaterialCode
                                          ,MaterialUnit = @MaterialUnit
                                          ,Num = @Num
                                          ,UnitCost = @UnitCost
                                          ,Cost = @Cost
                                          ,Purpose = @Purpose
                                          ,Remark = @Remark
                                          ,DispatchCode = @DispatchCode
                                          ,MissileCode = @MissileCode
                                          ,ProcessSortNumber = @ProcessSortNumber
                                          ,MRType = @MRType
                                     WHERE ID = @ID";

            try
            {

                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql_select);
                database.AddInParameter(dbCommand, "@ID", DbType.String, this.ID);
                string count = database.ExecuteScalar(dbCommand).ToString();

                if (count.Equals("0"))
                {
                    dbCommand = database.GetSqlStringCommand(sql_insert);
                }
                else
                {
                    dbCommand = database.GetSqlStringCommand(sql_update);
                }

                database.AddInParameter(dbCommand, "@ID", DbType.String, this.ID);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, this.FlowInstanceID);
                database.AddInParameter(dbCommand, "@MaterialName", DbType.String, hidMaterialName.Value);
                database.AddInParameter(dbCommand, "@MaterialCode", DbType.String, hidMaterialCode.Value);
                database.AddInParameter(dbCommand, "@MaterialUnit", DbType.String, hidMaterialUnit.Value);
                database.AddInParameter(dbCommand, "@Num", DbType.String, txt_Num.Text);
                database.AddInParameter(dbCommand, "@UnitCost", DbType.String, txt_UnitCost.Text);
                database.AddInParameter(dbCommand, "@Cost", DbType.String, txt_Cost.Text);
                database.AddInParameter(dbCommand, "@Purpose", DbType.String, txt_Purpose.Text);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, txt_Remark.Text);
                database.AddInParameter(dbCommand, "@DispatchCode", DbType.String, hidDispatchCode.Value);
                database.AddInParameter(dbCommand, "@MissileCode", DbType.String, hidMissileCode.Value);
                database.AddInParameter(dbCommand, "@ProcessSortNumber", DbType.String, hidProcessSortNumber.Value);
                database.AddInParameter(dbCommand, "@MRType", DbType.String, txtMRType.Text);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.FlowInstanceID))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
                return;
            }

            try
            {
                UpdateData();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
        }
    }
}